package gam.n1.skills;


import gam.n1.areas.Area;
import gam.n1.e.s.PlaySkillEndEvent;
import gam.n1.e.s.PlaySkillEvent;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

public abstract class BaseSkill implements Skill {
    private final Area a;
    private final UUID u;
    private final String n;
    private long sT; // 技能开始时间
    private long aT; // 技能持续时间

    private boolean s; // is Auto
    private Function<Skill,Double> d; // damageSet
    public BaseSkill(@NotNull Area a,UUID u, @NotNull String n){
        this.a = a;
        this.u = u;
        this.n = n;
    }

    @Override
    public void tick() {
        PlaySkillEvent var1 = new PlaySkillEvent(this);
        Bukkit.getPluginManager().callEvent(var1);
        if (!var1.isCancelled() && a.isRunning()) {
            this.sT = System.currentTimeMillis();
            _do();
            this.aT = System.currentTimeMillis() - this.sT;
            PlaySkillEndEvent var2 = new PlaySkillEndEvent(TimeUnit.SECONDS.convert(aT,TimeUnit.MILLISECONDS));
            Bukkit.getPluginManager().callEvent(var2);
        }
    }
    protected void _do(){
        // do something
    }
    @Override
    public String name() {
        return n;
    }
    @Override
    public UUID key() {
        return u;
    }

    @Override
    public Area area() {
        return a;
    }

    @Override
    public double damage() {
        return d != null ? d.apply(this) : 0;
    }

    @Override
    public boolean isRunning() {
        return true;
    }

    @Override
    public void setAuto(boolean a) {
        this.s = a;
    }

    public Function<Skill, Double> damageR() {
        return d;
    }

    @Override
    public void setDamageRule(Function<Skill, Double> f) {
        this.d = f;
    }

    @Override
    public boolean isAuto() {
        return s;
    }
}
